package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context
import android.widget.EditText
import android.widget.LinearLayout


import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.KeyboardUtils
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.model.bean.JlBean
import com.haoshuang.zhouzhoubang.ui.adapter.JlAdapter3
import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible


class JdDialog(context: Context, private val onSubmit: (str: String) -> Unit) :
    BottomPopupView(context) {
    override fun getImplLayoutId(): Int = R.layout.dialog_jd
    var jl = "0"
    override fun initPopupContent() {
        super.initPopupContent()
        var list = arrayListOf<JlBean>()
        list.add(JlBean("5km",0))
        list.add(JlBean("10km",0))
        list.add(JlBean("20km",0))
        list.add(JlBean("30km",0))
        list.add(JlBean("40km",0))
        list.add(JlBean("无限制",0))
        list.add(JlBean("其他",0))

        val flexboxLayoutManager1 = FlexboxLayoutManager(context)
        flexboxLayoutManager1.flexDirection = FlexDirection.ROW
        flexboxLayoutManager1.flexWrap = FlexWrap.WRAP   //按正常方向换行
        flexboxLayoutManager1.justifyContent = JustifyContent.FLEX_START //交叉轴的起点对齐。
        var adapter = JlAdapter3(list)
        var tvGb2 = findViewById<TextView>(R.id.gb)
        var tvSu = findViewById<EditText>(R.id.tvSu)

        var tvButton = findViewById<TextView>(R.id.tvButton)
        var llJl = findViewById<LinearLayout>(R.id.llJl)
        var rvList = findViewById<RecyclerView>(R.id.rvList)
        rvList.layoutManager = flexboxLayoutManager1
        rvList.adapter = adapter
        adapter.setOnItemClick { view, position, item ->
            if (list[position].isCheck == 0) {
                for (b in list) {
                    b.isCheck = 0
                }
                list[position].isCheck = 1
                if (position == 5) {
                    llJl.visible()
                } else {
                    llJl.gone()
                }
                when (position) {
                    0->{ jl="5" }
                    1->{jl="10"}
                    2->{jl="20"}
                    3->{jl="30"}
                    4->{jl="40"}
                    5->{jl="0"}
                    6->{jl="-1"}
                }
            } else {
                jl = "0"
                llJl.gone()
                list[position].isCheck = 0
            }
            adapter.notifyDataSetChanged()
        }
        tvGb2.setOnClickListener {
            dismiss()
        }
        tvButton.setOnClickListener {
            dismiss()
            if (jl == "-1") {
                jl = tvSu.text.toString()
                if (jl.isNullOrBlank()) {
                    toast("请选择接单距离")
                    return@setOnClickListener
                }
            }
            onSubmit(jl)
        }

    }


}